package com.jymj.service.impl;

import com.jymj.dao.GfDao;
import com.jymj.dao.MlxcDao;
import com.jymj.entity.DlEntity;
import com.jymj.entity.GfEntity;
import com.jymj.entity.fhtnty.GfFhEntity;
import com.jymj.entity.fhtnty.MlxcFhEntity;
import com.jymj.service.GfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * Created by dl on 2019/5/17.
 */
@Service
public class GfServiceImpl implements GfService {

    @Autowired
    private GfDao gfDao;
    @Resource
    private MlxcDao mlxcDao;

    @Override
    public List<GfFhEntity> queryByTzlx(Integer pjid) {
        List<GfFhEntity> gfFhEntityList = new ArrayList<>();

        List<GfEntity> dlEntities = gfDao.queryAllByTzlx(pjid);
        List<GfEntity> gfEntities = new ArrayList<>();
        try {
            gfEntities = gfDao.queryGfByTzlx(pjid);
        } catch (Exception e) {

        }
        Map<String, List<GfEntity>> gfmap = gfEntities.stream().collect(Collectors.groupingBy(GfEntity::getTzlx, LinkedHashMap::new, Collectors.toList()));
        Map<String, List<GfEntity>> dlmap = dlEntities.stream().collect(Collectors.groupingBy(GfEntity::getTzlx, LinkedHashMap::new, Collectors.toList()));
        Object[] array;
        if (dlEntities.size() > gfEntities.size()) {
            array = dlmap.keySet().toArray();
        } else {
            array = gfmap.keySet().toArray();
        }
        for (Object array1 : array) {
            GfFhEntity gfFhEntity = new GfFhEntity();
            String proj = array1 + "";
            gfFhEntity.setProj(proj);
            gfFhEntity.setDlEntityList(dlmap.get(proj) == null ? new ArrayList<>() : dlmap.get(proj));
            gfFhEntity.setGfEntities(gfmap.get(proj) == null ? new ArrayList<>() : gfmap.get(proj));
            gfFhEntityList.add(gfFhEntity);
        }

        return gfFhEntityList;
    }

    @Override
    public List<GfEntity> selectByName(String name, Integer pjid) {
        return gfDao.selectByName(name, pjid);
    }

    @Override
    public List<MlxcFhEntity> queryMlxc(String code,Integer type) {
        Map map = new HashMap();
        map.put("code",code);
        map.put("type",type);
        List<DlEntity> lhbwEntities = mlxcDao.queryLhbw(map);
        List<DlEntity> lhEntities = mlxcDao.queryLh(map);
        List<DlEntity> gfEntities = mlxcDao.queryGf(map);
        List<DlEntity> dlEntities = mlxcDao.queryDl(map);
        lhbwEntities.addAll(dlEntities);
        lhbwEntities.addAll(lhEntities);
        lhbwEntities.addAll(gfEntities);
        List<MlxcFhEntity> mlxcFhEntities = new ArrayList<>();
        Object[] array = null;
        Map<String,List<DlEntity>> map1 = new HashMap<>();
        if (type == 1){
            //按年划分
            map1 = lhbwEntities.stream().sorted(Comparator.comparing(DlEntity :: getXzqmc))
                    .collect(Collectors.groupingBy(DlEntity::getYear,LinkedHashMap::new,Collectors.toList()));
        }else if (type == 2){
            //按资金来源划分
            map1 = lhbwEntities.stream().sorted(Comparator.comparing(DlEntity::getXzqmc))
                    .collect(Collectors.groupingBy(DlEntity::getZjly,LinkedHashMap::new,Collectors.toList()));
        }else {
            //按项目类型划分
            map1 = lhbwEntities.stream().sorted(Comparator.comparing(DlEntity::getXzqmc))
                    .collect(Collectors.groupingBy(DlEntity::getLx,LinkedHashMap::new,Collectors.toList()));
        }
        array = map1.keySet().toArray();
        for (Object array1 : array){
            MlxcFhEntity mlxcFhEntity = new MlxcFhEntity();
            String proj = array1 + "";
            mlxcFhEntity.setXzqmc(proj);
            if (map1.get(proj) != null){
                mlxcFhEntity.setDlEntities(map1.get(proj));
                BigDecimal zj = new BigDecimal(0);
                BigDecimal sqzbt = new BigDecimal(0);
                BigDecimal czzj = new BigDecimal(0);
                BigDecimal xmgm = new BigDecimal(0);
                for (DlEntity dlEntity : map1.get(proj)){
                    zj = zj.add(dlEntity.getZj()== null ? new BigDecimal(0) : dlEntity.getZj());
                    sqzbt = sqzbt.add(dlEntity.getSqzbt()== null ? new BigDecimal(0) : dlEntity.getSqzbt());
                    czzj = czzj.add(dlEntity.getCzzj()== null ? new BigDecimal(0) : dlEntity.getCzzj());
                    xmgm = xmgm.add(dlEntity.getXmgm()== null ? new BigDecimal(0) : dlEntity.getXmgm());
                }
                mlxcFhEntity.setZj(zj);
                mlxcFhEntity.setSqzbt(sqzbt);
                mlxcFhEntity.setCzzj(czzj);
                mlxcFhEntity.setXmgm(xmgm);
            }
            mlxcFhEntities.add(mlxcFhEntity);
        }
        return mlxcFhEntities;
    }

    @Override
    public List<MlxcFhEntity> queryKdk() {
        return mlxcDao.queryKdk();
    }
}
